package com.xiaohao.communication.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.xiaohao.communication.bean.UsersBaseInfo;
import com.xiaohao.communication.bean.WxOpenId;
import com.xiaohao.communication.config.WxMaConfiguration;
import com.xiaohao.communication.service.UsersBaseInfoService;
import com.xiaohao.communication.utils.JsonUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
*
* 用户基本信息模块（说明：用户头像写死）
* */
@Controller
public class UserBaseInfoController {

    @Autowired
    UsersBaseInfoService usersBaseInfoService;

    /**
     * 微信小程序登陆
     * @param code
     * @return
     */
    @RequestMapping("/wxlogin")
    @ResponseBody
    public String login(String code) {
        String appid = "wx419fe93850141434";
        if (StringUtils.isBlank(code)) {
            return "empty jscode";
        }
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            //唯一用户凭证，相当于用户名
            final String username = session.getOpenid();

            //将微信openId 持久化了
            WxOpenId wxOpenId = new WxOpenId(username);
            usersBaseInfoService.wxLogin(wxOpenId);

            return JsonUtils.toJson(
                    session.getOpenid()
            );
        } catch (WxErrorException e) {
            return e.toString();
        }
    }

    //用户登录      success
    @RequestMapping("/login")
    @ResponseBody
    public String loginUser(UsersBaseInfo usersBaseInfo){
        Boolean a = usersBaseInfoService.loginUser(usersBaseInfo);
        if (a == true){
            return "success";
        }else{
            return "false";
        }
    }


    //用户注册      success
    @ResponseBody
    @GetMapping("/register")
    public String registerUser(UsersBaseInfo usersBaseInfo){
        usersBaseInfoService.registerUser(usersBaseInfo);
        return "success";
    }

    //用户注销      ****
    @ResponseBody
    @GetMapping("/logout")
    public String logoutUser(){

        return "redirect:/XXXX";
    }

    //用户查询所有信息  success
    @ResponseBody
    @GetMapping("/userinfo")
    public UsersBaseInfo getUserInfo(int id){
        UsersBaseInfo userInfo = usersBaseInfoService.getUserInfo(id);
        System.out.println(userInfo);
        return userInfo;
    }

    //模糊查询用户        success
    @ResponseBody
    @GetMapping("query-user")
    public List<UsersBaseInfo> getUserInfoByVagueUsername(String username){
        List<UsersBaseInfo> userInfoByVagueUsername = usersBaseInfoService.getUserInfoByVagueUsername(username);
        return userInfoByVagueUsername;
    }




}
